import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Login = () =>
  import('../views/login/Login')
const Guide = () =>
  import('../views/guide/Guide')
const Welcome = () =>
  import('../views/guide/guide-components/Welcome')
const Manage = () =>
  import('../views/manage/Manage')
const Detail = () =>
  import('../views/detail/Detail')
const Screen = () =>
  import('../views/screen/Screen')
const Auditing = () =>
  import('../views/auditing/Auditing')
const AllAuditing = () =>
  import('../views/auditing/AllAuditing')
const BackAuditing = () =>
  import('../views/auditing/BackAuditing')
const Echarts = () =>
  import('../views/echarts/Echarts')
const ChangeRules = () =>
  import('../views/screen/ChangeRules')
const All = () =>
  import('../views/detail/All')
// const AddPicture = () =>
//     import ('../views/profile/profile_child/AddPicture')

const routes = [{
  path: '',
  redirect: '/login'
},
{
  path: '/login',
  component: Login
},
{
  path: '/guide',
  component: Guide,
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    component: Welcome
  },
  {
    path: '/manage',
    component: Manage
  },
  {
    path: '/detail',
    component: Detail
  },
  {
    path: '/screen',
    component: Screen
  },{
    path: '/auditing',
    component: Auditing
  },{
    path: '/allauditing',
    component: AllAuditing
  },{
    path: '/backauditing',
    component: BackAuditing
  },{
    path: '/changeRules',
    component: ChangeRules
  },
  {
    path: '/all',
    component: All
  },]
},
{
  path: '/echarts',
  component: Echarts
},

]

const router = new VueRouter({
  routes,
  mode: 'history',
})


const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err);
}

router.beforeEach((to, from, next) => {
  if(to.path == '/login'){
    return next()
  }

  const tokenstr = window.sessionStorage.getItem('token')
  if(!tokenstr){
    return next('/login')
  }else{
    next()
  }

})

export default router

// import { createRouter, createWebHistory } from 'vue-router'

// const Home = () =>
//     import ('../views/home/Home.vue')
// const Category = () =>
//     import ('../views/category/Category.vue')
// const Profile = () =>
//     import ('../views/profile/Profile.vue')
// const Select = () =>
//     import ('../views/select/Select.vue')

// const routes = [{
//         path: '/',
//         redirect: './home'
//     },
//     {
//         path: './home',
//         component: Home
//     },
//     {
//         path: './category',
//         component: Category
//     },
//     {
//         path: './profile',
//         component: Profile
//     },
//     {
//         path: './select',
//         component: Select
//     }
// ]

// const routerHistory = createWebHistory()
// const router = createRouter({
//     history: routerHistory,
//     routes
// })
// export default router